/**
 * Created by Administrator on 2017/10/12.
 */

$(document).ready(function () {

    var id = locationSearcher('id');
    var detailTpl = $('#detail-template').html();
    var detailCmp = Handlebars.compile(detailTpl);
    var shopName = '';

    var loading = dialog.loading();
    
    $.ajax({
        url:C.interface.catDetail,
        type:'get',
        dataType:'json',
        data:{
            id:id,
            token: C.token
        },
        complete:function () {
            loading.close();
        },
        success:function (response) {
            if (response.errorCode == '200'){
                var data = response.data;

                shopName = data.name;
                var id=data.id;
                //收藏的数据
                var attention=data.attention;
                $('#detail-box').html(detailCmp(data));
                //判断店铺有没有被收藏过  >0 收藏了   =0没有收藏
                if(attention>0){
                    $('.shop-collect').addClass('shop-collect1')
                }

                //收藏商品
                $('.shop-collect').click(function () {

                    var loading = dialog.loading();
                    var _this=$(this);
                    $.ajax({
                        url: C.interface.add,
                        type: 'post',
                        dataType: 'json',
                        data: {
                            token: C.token,
                            storeId:id
                        },
                        complete:function () {
                            loading.close();
                        },
                        success:function (response) {
                            if (response.errorCode == '200'){
                                if(!_this.hasClass('shop-collect1') == true) {
                                    _this.addClass('shop-collect1');
                                    dialog.tusiSuccess('收藏成功',1000);
                                }else {
                                    _this.removeClass('shop-collect1')
                                    dialog.tusiSuccess('已取消收藏',1000);

                                }
                            }else {
                                dialog.tusiError(response.errorMsg);
                            }
                        }
                    })
                });

                //支付弹窗
                $('.shop-pay').click(payMethod);
                //分享弹窗
                $('.shop-share').click(shaMethod);

                $('#phone').click(function (e) {
                    e.preventDefault();
                    var phone = $(this).data('tel');
                    console.log(phone);

                    var ua = navigator.userAgent.toLowerCase();
                    if (/iphone|ipad|ipod/.test(ua)) {
                        iosCallPhone(phone);
                    } else {
                        androidCallPhone(JSON.stringify(phone));
                    }
                });
            }
        }
    });

    //支付弹窗
    function payMethod() {
        $('.mask').fadeIn();
        $('#pat-method').slideDown();
    }
    //分享弹窗
    function shaMethod() {
        $('.mask').fadeIn();
        $('#share-method').slideDown();
    }


    //当mask弹出之后，阻止底层向上滑动
    var mask = $('.mask');
    mask.bind('touchmove',function (event) {
        event.preventDefault();
    });
    $('#pat-method').bind('touchmove',function (event) {
        event.preventDefault();
    });
    $('#share-method').bind('touchmove',function (event) {
        event.preventDefault();
    });
    $('#pay-detail').bind('touchmove',function (event) {
        event.preventDefault();
    });
    //点击蒙版弹框的隐藏
    mask.bind('click',function () {
        mask.fadeOut();
        $('#share-method').slideUp();
        $('#pat-method').slideUp();
        $('#pay-detail').slideUp();
    });

    var method = null;


    $('#pay-submit').click(function () {
        method = $('input[name=radio1]:checked').val();
        $('#pat-method').slideUp();
        $('#pay-detail').slideDown();


    });
    $('#detail-submit').unbind().bind('click',function () {
        var inputSale = $('.actual-price').val();
        var redPacketSale = $('.hongbao-price').val();
        if (inputSale == ''){
            dialog.tusiError('输入的金额不能为空');
            return false;
        }
        if (redPacketSale == ''){
            redPacketSale = 0;
        }
        /**
         *支付。
         * 获取支付方式
         * @method 1为微信  2为支付宝
         * @redPacketSale  红包金额
         * @inputSale  输入的金额
         *
         * */
        goPay(inputSale,redPacketSale,method,'goBack',id,shopName);
    });

    

    function goBack(back) {
        dialog.tusiSuccess('hahhaha你调到我了');
        alert(back);
        window.history.go(-1);
    }


    //分享
    $('.share-list').unbind().bind('click',share);

    /**
     *
     * 调用原生分享方法，
     * @method 1为微信好友，2为朋友圈，3为QQ好友。
     *
     * */
    function share() {
        var method = $(this).data('method');
        console.log(method);
        var data = {
            method:method
        };
        var ua = navigator.userAgent.toLowerCase();
        if (/iphone|ipad|ipod/.test(ua)) {
            iosShare(data);
        } else {
            androidShare(JSON.stringify(data));
        }
    }

    /**
     * 安卓拨打电话
     * */
    function androidCallPhone(data) {
        window.huifa.callPhone(data);
    }

    /**
     *
     * iOS 拨打电话*/

    function iosCallPhone(data) {
        window.webkit.messageHandlers.callPhone.postMessage(data);
    }

    /**
     * 安卓分享
     * */
    function androidShare(param) {
        window.huifa.pullShare(param);
    }

    /**
     * ios分享
     * */
    function iosShare(param) {
        window.webkit.messageHandlers.pullShare.postMessage(param);
    }


});

